package br.com.news.ws;

import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;

import br.com.news.utils.database.beans.AlternativaEnqueteBean;
import br.com.news.utils.database.beans.RespostaEnqueteBean;
import br.com.news.utils.database.beans.keys.AlternativaEnqueteKeyBean;
import br.com.news.utils.service.exception.ServiceException;

@Path("/resposta")
public class RespostaEnqueteWs extends GenericWs<RespostaEnqueteBean>{

	private static final Logger logger = Logger.getLogger(RespostaEnqueteWs.class);
	
	@POST
	@Path("/enquete/web/insert/{idEnquete}/{alternativa}")
	public void saveWeb(@PathParam(value="idEnquete") long idEnquete,@PathParam(value="alternativa") short alternativa) {
		try {
			AlternativaEnqueteBean alternativaEnqueteBean = new AlternativaEnqueteBean();
			AlternativaEnqueteKeyBean alternativaEnqueteKey = new AlternativaEnqueteKeyBean();
			alternativaEnqueteKey.setAlternativa(alternativa);
			alternativaEnqueteKey.setIdEnquete(idEnquete);
			
			alternativaEnqueteBean.setAlternativaEnqueteKey(alternativaEnqueteKey);
						
			RespostaEnqueteBean respostaEnquete = new RespostaEnqueteBean();			
			respostaEnquete.setDataResposta(new Date());
			respostaEnquete.setAlternativaEnquete(alternativaEnqueteBean);
			RmiFactory.getRespostaEnqueteService().save(respostaEnquete);
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
		}
	}
	
	@GET
	@Path("/enquete/insert/{idEnquete}/{alternativa}")
	public void save(@PathParam(value="idEnquete") long idEnquete,@PathParam(value="alternativa") short alternativa) {
		try {
			AlternativaEnqueteBean alternativaEnqueteBean = new AlternativaEnqueteBean();
			AlternativaEnqueteKeyBean alternativaEnqueteKey = new AlternativaEnqueteKeyBean();
			alternativaEnqueteKey.setAlternativa(alternativa);
			alternativaEnqueteKey.setIdEnquete(idEnquete);
			
			alternativaEnqueteBean.setAlternativaEnqueteKey(alternativaEnqueteKey);
						
			RespostaEnqueteBean respostaEnquete = new RespostaEnqueteBean();			
			respostaEnquete.setDataResposta(new Date());
			respostaEnquete.setAlternativaEnquete(alternativaEnqueteBean);
			RmiFactory.getRespostaEnqueteService().save(respostaEnquete);
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
		}
	}
	
	@GET
	@Path("/xml/result/vote/{idEnquete}")
	@Produces(MediaType.APPLICATION_XML)
	public List<RespostaEnqueteBean> resultVoteXml(@PathParam(value="idEnquete") long idEnquete){
		try {
			return RmiFactory.getRespostaEnqueteService().resultVoteXml(idEnquete);
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			return null;
		}
	}
	
	@GET
	@Path("/result/vote/{idEnquete}")
	@Produces(MediaType.APPLICATION_JSON)
	public String resultVote(@PathParam(value="idEnquete") long idEnquete){
		try {
			return listToJson(RmiFactory.getRespostaEnqueteService().resultVoteXml(idEnquete));
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			return "NOK";
		}
	}
}
